//
//  PersonDataViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/5.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "PersonDataViewController.h"
#import "UserModel.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "CloseAreaViewController.h"
#import "BasicInformationViewController.h"
#import "WorkExperienceViewController.h"
#import "EducationExperienceViewController.h"
#import "CertificatePhotosViewController.h"
#import "ResumeModel.h"
#import "cerImageModel.h"
#import "ExperiModel.h"

static NSString *leftCellPath = @"inforcell";
static NSString *rightCellPath = @"jianlicell";
@interface PersonDataViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
{
    UITextField *lnickNamefiled;
    UITextField *lpricefiled;
    CGFloat scrollViewOffSetY;
    CGFloat remainingDistanceY;
    CGFloat beforeMigrationY;
    UIButton *rightSaveBtn;
}
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UITableView *leftTableview;
@property (nonatomic, strong)UITableView *rightTableview;
@property (nonatomic, strong)UIView *theLineView;
@property (nonatomic, strong)NSArray *leftTitAry;
@property (nonatomic, strong)NSArray *rightTitAry;
@property (nonatomic, strong)NSArray *rightImgAry;
@property (nonatomic, strong)NSString *leftNameStr;
@property (nonatomic, strong)NSString *leftPriceStr;
@property (nonatomic, strong)NSArray *workExperienceAry;
@property (nonatomic, strong)NSArray *educationAry;
@property (nonatomic, strong)NSArray *certificateAry;
@property (nonatomic, strong)UIImage *faceImg;
@property (nonatomic, strong)UIImageView *faceView;
@property (nonatomic, strong)NSString *faceString;
@property (nonatomic, strong)UILabel *addressLabel;
@property (nonatomic, strong)UIImage *jianliImg;
@property (nonatomic, strong)UserModel *userModel;
@property (nonatomic, assign)NSInteger closeImgType;
@property (nonatomic, strong)NSString *areaId;
@property (nonatomic, strong)NSString *siteStr;
@property (nonatomic, strong)ResumeModel *resuModel;

@end

@implementation PersonDataViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpantanchu:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpanhuishou:) name:UIKeyboardDidHideNotification object:nil];
    if (_closeImgType != 1)
    {
        [self requestGetWorkerInfo];

    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    _closeImgType = 1;
    _leftNameStr = @"";
    _leftPriceStr = @"";
    _faceString = @"";
    _areaId = @"";
    _siteStr = @"";
    _jianliImg = nil;
    _faceImg = nil;
    [self creatUI];
    [self requestGetUserInfo];
    [self requestGetWorkerInfo];
}

- (void)creatUI
{
    self.navigationItem.title = @"个人信息";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    UIButton *rightBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, 40, 30) ImageName:nil Target:self Action:@selector(saveAction) Title:@"保存" Font:16.0];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = barbtn;
    rightSaveBtn = rightBtn;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    self.theLineView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth)];
    _theLineView.backgroundColor = RGB(232, 80, 75);
    [topView addSubview:_theLineView];
    NSArray *titAry = @[@"信息",@"简历"];
    for (int i = 0; i < titAry.count; i++)
    {
        UIButton *botton = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth+(SCREEN_WIDTH/2)*i, 5*displaywidth, SCREEN_WIDTH/2-40*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(closeTypeAction:) Title:titAry[i] Font:14.0];
        [botton setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        botton.tag = 10+i;
        [topView addSubview:botton];
    }
    CGFloat height = SCREEN_HEIGHT-STA_NAV_HEIGHT-40*displaywidth-SCREER_HeiX;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40*displaywidth, SCREEN_WIDTH, height)];
    _scrollView.delegate = self;
    _scrollView.backgroundColor = BacksColor;
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.pagingEnabled = NO;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, height);
    _scrollView.scrollEnabled = NO;
    [self.view addSubview:_scrollView];
    _leftTitAry = @[@"头像",@"昵称",@"ID"];//,@"报价",@"常住地"
    self.leftTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    _leftTableview.backgroundColor = BacksColor;
    _leftTableview.delegate = self;
    _leftTableview.dataSource = self;
    _leftTableview.tag = 20;
    [_leftTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [_scrollView addSubview:_leftTableview];
    _rightTitAry = @[@"基本情况",@"教育经历",@"工作经历",@"证书"];
    _rightImgAry = @[@"jibenqingk",@"jiaoyujingli",@"gongzuojingli",@"zhengshu-"];
    self.rightTableview = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    _rightTableview.backgroundColor = BacksColor;
    _rightTableview.delegate = self;
    _rightTableview.dataSource = self;
    _rightTableview.tag = 22;
    [_rightTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [_scrollView addSubview:_rightTableview];
}

//选择信息或简历
- (void)closeTypeAction:(UIButton *)btn
{
    if (btn.tag == 10)
    {
        rightSaveBtn.hidden = NO;
        _closeImgType = 1;
        _theLineView.frame = CGRectMake(SCREEN_WIDTH/4-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth);
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else
    {
        rightSaveBtn.hidden = YES;
        _closeImgType = 2;
        _theLineView.frame = CGRectMake(SCREEN_WIDTH/4*3-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth);
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];

    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 22)
    {
        return _rightTitAry.count;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 22)
    {
        if (section == 0)
        {
            return 1;
        }
        else if (section == 1)
        {
            if (![_resuModel.school isEqualToString:@""])
            {
                return 1;
            }
            else
            {
                return 1;
            }
        }
        else if (section == 3)
        {
            return _resuModel.certificateImgs.count;
        }
        else if (section == 2)
        {
            return _resuModel.experiences.count;
        }
            
        return 10;
    }
    return _leftTitAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        NSString *cellpath = leftCellPath;
        if (tableView.tag == 22)
        {
            cellpath = rightCellPath;
        }
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellpath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 20)
    {
        cell.textLabel.text = _leftTitAry[indexPath.row];
        cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
        cell.textLabel.textColor = RGB(77, 77, 77);
        if (indexPath.row == 0)
        {
            UIImageView *faceImgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-105*displaywidth, 7.5*displaywidth, 75*displaywidth, 75*displaywidth) ImageName:@"zhanwei"];
            if (_faceImg)
            {
                faceImgView.image = _faceImg;
            }
            else
            {
                if (_userModel.headImg.length > 0)
                {
                    [faceImgView sd_setImageWithURL:[NSURL URLWithString:_userModel.headImg] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
                }
               
            }
            faceImgView.contentMode = UIViewContentModeScaleAspectFill;
            faceImgView.layer.cornerRadius = 37.5*displaywidth;
            faceImgView.layer.masksToBounds = YES;
            [cell.contentView addSubview:faceImgView];
            _faceView = faceImgView;
            
        }
        else if (indexPath.row == 2)
        {
            UILabel *idLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-170*displaywidth, 5*displaywidth, 150*displaywidth, 35*displaywidth) Font:13.0 Text:_userModel.mark Color:RGB(77, 77, 77)];
            idLab.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:idLab];
        }
//        else if (indexPath.row == 4)
//        {
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            UILabel *addressLab = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 5*displaywidth, SCREEN_WIDTH-130*displaywidth, 35*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"%@%@%@",_userModel.province,_userModel.city,_userModel.areaName] Color:RGB(77, 77, 77)];
//            if (![_siteStr isEqualToString:@""])
//            {
//                addressLab.text = _siteStr;
//            }
//            addressLab.textAlignment = NSTextAlignmentRight;
//            [cell.contentView addSubview:addressLab];
//            _addressLabel = addressLab;
//        }
        else
        {
            NSArray *pladerAry = @[@"",@"昵称",@"",@"元/小时",@""];
            NSString *priceStr = [NSString stringWithFormat:@"%@元/小时",_leftPriceStr];
            NSArray *textAry = @[@"",_leftNameStr,@"",priceStr,@""];
            UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(100*displaywidth, 5*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) placeholder:pladerAry[indexPath.row] passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
            [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
            textfield.textColor = RGB(77, 77, 77);
            textfield.text = textAry[indexPath.row];
            textfield.textAlignment = NSTextAlignmentRight;
            textfield.delegate = self;
            [cell.contentView addSubview:textfield];
            if (indexPath.row == 1)
            {
                lnickNamefiled = textfield;
            }
            if (indexPath.row == 3)
            {
                textfield.keyboardType = UIKeyboardTypeDecimalPad;
                lpricefiled = textfield;
            }

        }
    }
    else
    {
        if (indexPath.section == 0)
        {
            if (indexPath.row == 0)
            {
                UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(20*displaywidth, 10*displaywidth, 80*displaywidth, 100*displaywidth) ImageName:@"shangchuanzhanp"];
                if (_jianliImg)
                {
                    imgView.image = _jianliImg;
                }
                else
                {
                    if (![_resuModel.photo isEqualToString:@""])
                    {
                        [imgView sd_setImageWithURL:[NSURL URLWithString:_resuModel.photo] placeholderImage:[UIImage imageNamed:@"shangchuanzhanp"]];
                    }
                }
                imgView.backgroundColor = [UIColor lightGrayColor];
                imgView.contentMode = UIViewContentModeScaleAspectFill;
                imgView.layer.masksToBounds = YES;
                [cell.contentView addSubview:imgView];
                if (_resuModel)
                {
                    NSArray *sexAry = @[@"保密",@"男",@"女"];
                    NSString *sexStr = sexAry[[_resuModel.sex integerValue]];
                    NSArray *textArray = @[_resuModel.linkman,sexStr,_resuModel.phone];
                    for (int i = 0; i < 3; i++)
                    {
                        UILabel *label = [ZCControl createLabelWithFrame:CGRectMake(140*displaywidth, 7.5*displaywidth+35*displaywidth*i, SCREEN_WIDTH-160*displaywidth, 35*displaywidth) Font:13.0 Text:textArray[i] Color:RGB(77, 77, 77)];
                        label.textAlignment = NSTextAlignmentRight;
                        [cell.contentView addSubview:label];
                    }
                }
                
            }
            
        }
        else if (indexPath.section == 1)
        {
            if (![_resuModel.school isEqualToString:@""])
            {
                cell.textLabel.text = _resuModel.school.length>0?_resuModel.school:@"未填写";
                cell.textLabel.textColor = RGB(77, 77, 77);
                cell.textLabel.font = NormalFontWithSize(13.0+fontScale);
                UILabel *edulabel = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH -  100*displaywidth, 5*displaywidth, 80*displaywidth, 35*displaywidth) Font:13.0 Text:_resuModel.education Color:RGB(77, 77, 77)];
                edulabel.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:edulabel];
                

            }
            else
            {
                cell.textLabel.text = _resuModel.school.length>0?_resuModel.school:@"未填写";
                cell.textLabel.textColor = RGB(77, 77, 77);
                cell.textLabel.font = NormalFontWithSize(13.0+fontScale);
            }
        }
        else if (indexPath.section == 3)
        {
            if (_resuModel.certificateImgs.count > 0)
            {
                cerImageModel *cerModel = _resuModel.certificateImgs[indexPath.row];
                UIImageView *cerImgView = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200*displaywidth) ImageName:nil];
                [cerImgView sd_setImageWithURL:[NSURL URLWithString:cerModel.url]];
                cerImgView.contentMode = UIViewContentModeScaleAspectFit;
                [cell.contentView addSubview:cerImgView];
                UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(0, 200*displaywidth, SCREEN_WIDTH, 5*displaywidth)];
                lineAview.backgroundColor = BacksColor;
                [cell.contentView addSubview:lineAview];
            }
            
        }
        else if (indexPath.section == 2)
        {
            if (_resuModel.experiences.count > 0)
            {
                ExperiModel *expmodel = _resuModel.experiences[indexPath.row];
                cell.textLabel.text = expmodel.department;
                cell.textLabel.textColor = RGB(77, 77, 77);
                cell.textLabel.font = NormalFontWithSize(13.0+fontScale);
                NSString *startStr = [expmodel.startDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
                NSString *endStr = [expmodel.endDate stringByReplacingOccurrencesOfString:@"-" withString:@"."];
                UILabel *edulabel = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH/2, 5*displaywidth, SCREEN_WIDTH/2-20*displaywidth, 35*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"%@-%@",startStr,endStr] Color:RGB(77, 77, 77)];
                edulabel.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:edulabel];
            }
           
        }
//        if (indexPath.section == 2 && indexPath.row == 8)
//        {
//            UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(100*displaywidth, 5*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) placeholder:@"请输入" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
//            [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
//            textfield.textColor = RGB(77, 77, 77);
//            textfield.text = @"";
//            textfield.textAlignment = NSTextAlignmentRight;
//            textfield.delegate = self;
//            textfield.tag = 150;
//            [cell.contentView addSubview:textfield];
//        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WEAKSELF;
    if (tableView.tag == 20)
    {
        if (indexPath.row == 0)
        {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            //拍照
            [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                HYLog(@"拍照");
                _closeImgType = 1;
                [self takePhoto];
                
            }]];
            
            //从相册中选取
            [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                HYLog(@"从相册中选取");
                _closeImgType = 1;
                [self choseImageWithTypeCameraTypePhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
                
            }]];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                HYLog(@"取消");
                [self dismissViewControllerAnimated:NO completion:nil];
            }]];
            
            [self presentViewController:alertVC animated:YES completion:nil];
        }
        else if (indexPath.row == 4)
        {
            CloseAreaViewController *vc = [[CloseAreaViewController alloc] init];
            vc.chooseOnlyblock = ^(NSString *areaId, NSString *siteStr)
            {
                weakSelf.areaId = areaId;
                weakSelf.siteStr = siteStr;
                weakSelf.addressLabel.text = siteStr;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else
    {
        if (indexPath.section == 0)
        {
            BasicInformationViewController *VC = [[BasicInformationViewController alloc] init];
            VC.resuModel = _resuModel;
            [self.navigationController pushViewController:VC animated:YES];
        }
        else if (indexPath.section == 1)
        {
            EducationExperienceViewController *vc = [[EducationExperienceViewController alloc] init];
            vc.isEdit = YES;
            vc.schoolNameStr = _resuModel.school;
            vc.eduNameStr = _resuModel.education;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.section == 2)
        {
            WorkExperienceViewController *vc = [[WorkExperienceViewController alloc] init];
            vc.isEdit = YES;
            ExperiModel *model = _resuModel.experiences[indexPath.row];
            vc.experModel = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

-(void)takePhoto
{
    //    判断是否打开相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您尚未开启相机权限，请去系统设置界面打开相机权限！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url];
            }
        }];
        [alertVC addAction:okAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        
        
    }
    else
    {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            //            设置拍照后的图片可被编辑
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            
            [self presentViewController:picker animated:YES completion:nil];
        }else
        {
            
        }
        
    }
}

-(void)choseImageWithTypeCameraTypePhotoLibrary:(UIImagePickerControllerSourceType)type{
    
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = type;
    imagePicker.allowsEditing = YES;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImageOrientation imageOrientation = image.imageOrientation;
        if(imageOrientation!=UIImageOrientationUp)
        {
            UIGraphicsBeginImageContext(image.size);
            [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        
    }
    if (_closeImgType == 1)
    {
        _faceView.image = image;
        _faceImg = image;
    }
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HttpTool uploadImageWithPath:SERVER_fileUpload params:nil thumbName:@"file" image:image success:^(id json)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([json server_Getcodecheck])
         {
             NSArray *urlAry = json[@"data"];
             if (urlAry.count > 0)
             {
                 NSString *imgurl = urlAry[0];
                 if (imgurl.length > 0)
                 {
                     weakSelf.faceString = imgurl;
                 }

             }
         }

     } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];

     } progress:^(CGFloat progress) {

     }];
    
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//保存
- (void)saveAction
{
    [self.view endEditing:YES];
    if (_closeImgType == 1)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
        if (![_faceString isEqualToString:@""])
        {
            [dic setObject:_faceString forKey:@"headImg"];
        }
        if (![lnickNamefiled.text isEqualToString:@""])
        {
            [dic setObject:lnickNamefiled.text forKey:@"name"];
        }
        if ([_leftPriceStr floatValue] > 0)
        {
            [dic setObject:_leftPriceStr forKey:@"wage"];
        }
        if (![_areaId isEqualToString:@""])
        {
            NSDictionary *areaDic = @{@"id":_areaId};
            [dic setObject:areaDic forKey:@"area"];
//            [dic setObject:_siteStr forKey:@"site"];
        }
        [self requestUpdateInfo:dic];
    }
}

- (void)requestUpdateInfo:(NSDictionary *)dic
{
    WEAKSELF;
    [HYBaseHandler requestUpdateInfoParmers:dic Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"保存成功" addToView:sharedDelegate.window];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myview = [[UIView alloc] init];
    if (tableView.tag == 22)
    {
        myview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 35*displaywidth);
        myview.backgroundColor = [UIColor whiteColor];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5*displaywidth)];
        lineView.backgroundColor = BacksColor;
        [myview addSubview:lineView];
        UIImageView *imgview = [ZCControl createImageViewWithFrame:CGRectMake(10*displaywidth, 10*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:_rightImgAry[section]];
        imgview.contentMode = UIViewContentModeScaleAspectFit;
        [myview addSubview:imgview];
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(45*displaywidth, 5*displaywidth, 120*displaywidth, 30*displaywidth) Font:14.0 Text:_rightTitAry[section] Color:RGB(129, 148, 196)];
        [myview addSubview:titLab];
        UIView *lineBView = [[UIView alloc] initWithFrame:CGRectMake(0, 34*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineBView.backgroundColor = BacksColor;
        [myview addSubview:lineBView];
        NSString *imgStr = @"tianjia-";
        if (section == 0 || section == 1)
        {
            imgStr = @"gs_icon05";
        }
        UIImageView *rightImgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-35*displaywidth, 12.5*displaywidth, 15*displaywidth, 15*displaywidth) ImageName:imgStr];
        rightImgView.contentMode = UIViewContentModeScaleAspectFit;
        [myview addSubview:rightImgView];
        UIButton *addlistBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2, 5*displaywidth, SCREEN_WIDTH/2, 29*displaywidth) ImageName:nil Target:self Action:@selector(addListAction:) Title:nil Font:12.0];
        addlistBtn.tag = 50+section;
        addlistBtn.backgroundColor = [UIColor clearColor];
        [myview addSubview:addlistBtn];
    }
    else
    {
        myview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 5*displaywidth);
        myview.backgroundColor = BacksColor;
    }
    return myview;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 22)
    {
        return 35*displaywidth;
    }
    return 5*displaywidth;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 20)
    {
        if (indexPath.row == 0)
        {
            return 90*displaywidth;
        }
        return 45*displaywidth;
    }
    else
    {
        if (indexPath.section == 0)
        {
            return 120*displaywidth;
        }
        else if (indexPath.section == 3)
        {
            return 205*displaywidth;
        }
        return 45*displaywidth;
    }
}

//简历添加或编辑
- (void)addListAction:(UIButton *)btn
{
    NSInteger index = btn.tag-50;
    if (index == 0)
    {
        BasicInformationViewController *Vc = [[BasicInformationViewController alloc] init];
        Vc.resuModel = _resuModel;
        [self.navigationController pushViewController:Vc animated:YES];
    }
    else if (index == 1)
    {
        EducationExperienceViewController *Vc = [[EducationExperienceViewController alloc] init];
        if (![_resuModel.school isEqualToString:@""])
        {
            Vc.isEdit = YES;
            Vc.schoolNameStr = _resuModel.school;
            Vc.eduNameStr = _resuModel.education;
        }
        [self.navigationController pushViewController:Vc animated:YES];
    }
    else
    {
        if (index < 4)
        {
            NSArray *classArray = @[@"BasicInformationViewController", @"EducationExperienceViewController", @"WorkExperienceViewController",@"CertificatePhotosViewController"];
            
            if (index == 3) {
                CertificatePhotosViewController *VC = [[CertificatePhotosViewController alloc] init];
                VC.preImagesArr = _resuModel.certificateImgs;
                [self.navigationController pushViewController:VC animated:YES];
            } else {
                HYBaseViewController *VC = [[NSClassFromString(classArray[index]) alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
        }
    }
   
    
   
    
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    return indexPath;
}

// 我们获取当前tableView 的偏移量
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /* scrollView 的偏移量*/
    CGFloat offSetY = scrollView.contentOffset.y;
    scrollViewOffSetY = offSetY;
    
}
// 我们获取那个Cell是被点击的
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    /* 获取当前cell的位置*/
    /* 我们进行层次分析
     * A.superview 则是 cell
     */
//    if (textField.tag == 150)
//    {
//        CGFloat Hieght = textField.superview.superview.frame.origin.y;
//        /* 获取当前点击Cell的位置*/
//        CGFloat CurrentCellY = Hieght - scrollViewOffSetY;
//        /* 获取剩余高度*/
//        CGFloat lastHeight = [UIScreen mainScreen].bounds.size.height - CurrentCellY-200;
//        remainingDistanceY = lastHeight;
//    }
    if (textField == lpricefiled)
    {
        if ([textField.text containsString:@"元/小时"])
        {
            textField.text = [textField.text stringByReplacingOccurrencesOfString:@"元/小时" withString:@""];
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == lpricefiled)
    {
        if (![textField.text containsString:@"元/小时"])
        {
            if ([textField.text floatValue] > 0)
            {
                _leftPriceStr = textField.text;
            }
            textField.text = [textField.text stringByAppendingString:@"元/小时"];
        }
        
    }
}
// 我们在获取弹出键盘的高度和在做计算差值偏移
-(void)jianpantanchu:(NSNotification*)Notification{
    NSLog(@"%@",Notification);
    /* 获取键盘的开始高度*/
    NSDictionary *userInfo = [Notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyBoardStart = keyboardRect.size.height;
    
//    CGFloat keyBoardStart = [Notification.userInfo[@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue].size.height;
    if (remainingDistanceY < keyBoardStart) {
        /* 获取差值*/
        CGFloat differenceValue = keyBoardStart -remainingDistanceY;
        beforeMigrationY = differenceValue;
        [_rightTableview setContentOffset:CGPointMake(0, differenceValue + scrollViewOffSetY) animated:YES];
    }
}

// 我们为了防止tableView不能返回原始没有被点击的位置(这是防止用户在键盘弹出的时候，在滑动而造成的混乱)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    /* 防止错乱停止编辑*/
    [self.view endEditing:YES];
}
// 最后键盘隐藏，返回点击前的偏移
-(void)jianpanhuishou:(NSNotification*)Notification{
    NSLog(@"Hide");
    [_rightTableview setContentOffset:CGPointMake(0, scrollViewOffSetY-beforeMigrationY) animated:YES];
    beforeMigrationY = 0.0;
}

//个人资料
- (void)requestGetUserInfo
{
    WEAKSELF;
    [HYBaseHandler requestGetUserInfoParmers:nil Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            weakSelf.userModel = [UserModel mj_objectWithKeyValues:obj[@"data"]];
            _leftNameStr = weakSelf.userModel.name;
//            _leftPriceStr = weakSelf.userModel.wage;
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.leftTableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

//简历
- (void)requestGetWorkerInfo
{
    WEAKSELF;
    [HYBaseHandler requestGetWorkerInfoParmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             _resuModel = [ResumeModel mj_objectWithKeyValues:obj[@"data"]];
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf.rightTableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
 
